//
//  SASelectShopCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/4/9.
//

#import "SASelectShopCell.h"
#import "SASalesOrderViewListModel.h"

@interface SASelectShopCell ()

@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UILabel *shopIdLab;
@property (nonatomic, strong) UILabel *shopTypeLab;
@property (nonatomic, strong) UILabel *shopNameLab;
//@property (nonatomic, strong) UIButton *numBtn;
@property (nonatomic, strong) UILabel *numLab;

@end

@implementation SASelectShopCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASalesOrderViewListModel *)model {
    _model = model;

    NSString *newUrl = [[model.img componentsSeparatedByString:@","][0] stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
    NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_100",newUrl];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"shop_placeholderImg"]];
    self.shopIdLab.text = [NSString stringWithFormat:@"%@,%@",model.productCode,model.productName];
    self.shopTypeLab.text = [NSString stringWithFormat:@"零售价:%0.2lf",[model.retailPrice doubleValue]/100.f];
    self.shopNameLab.text = [NSString stringWithFormat:@"库存:%@",model.totalStock];
    NSInteger num = 0;
    for (SASalesOrderToPendSkuList *list in model.toPendSkuAttrs) {
        num += [list.stock integerValue];
    }
    if (num > 0) {
        self.numLab.hidden = NO;
        self.numLab.text = [NSString stringWithFormat:@"%zd",num];
    }else {
        self.numLab.hidden = YES;
    }
}

-(void)setAllocateModel:(SASalesOrderViewListModel *)allocateModel {
    _allocateModel = allocateModel;
    
    NSString *newUrl = [[allocateModel.img componentsSeparatedByString:@","][0] stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
    NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_100",newUrl];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"shop_placeholderImg"]];
    self.shopIdLab.text = [NSString stringWithFormat:@"%@,%@",allocateModel.productCode,allocateModel.productName];
    self.shopTypeLab.text = [NSString stringWithFormat:@"入库价:%0.2lf",[allocateModel.retailPrice doubleValue]/100.f];
    self.shopNameLab.text = [NSString stringWithFormat:@"库存:%@",allocateModel.totalStock];
    NSInteger num = 0;
    for (SASalesOrderToPendSkuList *list in allocateModel.toPendSkuAttrs) {
        num += [list.stock integerValue];
    }
    if (num > 0) {
        self.numLab.hidden = NO;
        self.numLab.text = [NSString stringWithFormat:@"%zd",num];
    }else {
        self.numLab.hidden = YES;
    }
    
    NSString *productViewCostPrice = [NSString stringWithFormat:@"%@",[SAValueManger shareInStance].userInfo.menuList[1][@"perms"][@"productViewCostPrice"]];
    if ([productViewCostPrice isEqualToString:@"1"]) {
        self.shopTypeLab.text = [NSString stringWithFormat:@"入库价:******"];
    }
}

-(void)setWareModel:(SASalesOrderViewListModel *)wareModel {
    _wareModel = wareModel;

    NSString *newUrl = [[wareModel.img componentsSeparatedByString:@","][0] stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
    NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_100",newUrl];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"shop_placeholderImg"]];
    self.shopIdLab.text = [NSString stringWithFormat:@"%@,%@",wareModel.productCode,wareModel.productName];
    self.shopTypeLab.text = [NSString stringWithFormat:@"入库价:%0.2lf",[wareModel.entryPrice doubleValue]/100.f];
    self.shopNameLab.text = [NSString stringWithFormat:@"库存:%@",wareModel.totalStock];
    NSInteger num = 0;
    for (SASalesOrderToPendSkuList *list in wareModel.toPendSkuAttrs) {
        num += [list.stock integerValue];
    }
    if (num == 0) {
        self.numLab.hidden = YES;
    }else {
        self.numLab.hidden = NO;
        self.numLab.text = [NSString stringWithFormat:@"%zd",num];
    }
    
    NSString *productViewCostPrice = [NSString stringWithFormat:@"%@",[SAValueManger shareInStance].userInfo.menuList[1][@"perms"][@"productViewCostPrice"]];
    if ([productViewCostPrice isEqualToString:@"1"]) {
        self.shopTypeLab.text = [NSString stringWithFormat:@"入库价:******"];
    }
}
-(void)clickedImageV {
    !self.showImageBlock ?: self.showImageBlock();
}

#pragma mark - initUI
-(void)initUI {

    [self.contentView addSubview:self.imageV];
    [self.contentView addSubview:self.shopIdLab];
    [self.contentView addSubview:self.shopTypeLab];
    [self.contentView addSubview:self.shopNameLab];
    [self.contentView addSubview:self.numLab];
    [self.contentView addSubview:self.line];
    
    [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kScaleNum(22));
        make.centerY.mas_equalTo(kScaleNum(0));
        make.right.mas_equalTo(kScaleNum(-22));
        make.width.mas_greaterThanOrEqualTo(kScaleNum(22));
    }];
}

#pragma mark - lazy
-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:SARectMake(26, 6, 53, 59)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedImageV)];
        _imageV.userInteractionEnabled = YES;
        _imageV.contentMode = UIViewContentModeScaleAspectFit;
        _imageV.clipsToBounds = YES;
        [_imageV setContentScaleFactor:[[UIScreen mainScreen] scale]];
        [_imageV addGestureRecognizer:tap];
    }
    return _imageV;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:SARectMake(0, 70, 375, 1)];
        _line.backgroundColor = kLineColor;
    }
    return _line;
}

-(UILabel *)shopIdLab {
    if (!_shopIdLab) {
        _shopIdLab = [[UILabel alloc] initWithFrame:SARectMake(85, 5, 150, 20)];
        _shopIdLab.textColor = [UIColor blackColor];
        _shopIdLab.font = kFont(13);
    }
    return _shopIdLab;
}

-(UILabel *)shopTypeLab {
    if (!_shopTypeLab) {
        _shopTypeLab = [[UILabel alloc] initWithFrame:SARectMake(85, 25, 150, 20)];
        _shopTypeLab.textColor = kLightgGrayColor;
        _shopTypeLab.font = kFont(13);
    }
    return _shopTypeLab;
}

-(UILabel *)shopNameLab {
    if (!_shopNameLab) {
        _shopNameLab = [[UILabel alloc] initWithFrame:SARectMake(85, 45, 150, 20)];
        _shopNameLab.textColor = kLightgGrayColor;
        _shopNameLab.font = kFont(13);
    }
    return _shopNameLab;
}

//-(UIButton *)numBtn {
//    if (!_numBtn) {
//        _numBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_numBtn setBackgroundImage:[UIImage imageNamed:@"shop_redRound"] forState:UIControlStateNormal];
//        _numBtn.frame = SARectMake(332, 25, 21, 21);
//        [_numBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        _numBtn.titleLabel.font = kFont(12);
//    }
//    return _numBtn;
//}

-(UILabel *)numLab {
    if (!_numLab) {
        _numLab = [[UILabel alloc] init];
        _numLab.textColor = [UIColor whiteColor];
        _numLab.backgroundColor = kPinkColor;
        _numLab.font = kFont(13);
        _numLab.textAlignment = NSTextAlignmentCenter;
        _numLab.layer.cornerRadius = kScaleNum(11);
        _numLab.layer.masksToBounds = YES;
    }
    return _numLab;
}


@end
